<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Cli\Controller;

use \Think\Controller;

/**
 * @controller 过期前一个月发送短信
 */
class ExpireController extends Controller {

    /**
     * crontab执行,HTTP请求数据更新
     * 每日2点执行
     * @modify zhangzhen 2017-06-14 
     */
    public function run() {
        if (!IS_CLI) {
            exit;
        }
        $hotelEntityModel = D('HotelEntity');
        $adminLoginModel = D('AdminLogin');
        $adminHotelModel = D('AdminHotel');
        $nowdate = strtotime(date('Y-m-d'));
        $i = 0;
        while ($hotelEntityInfos = $hotelEntityModel->limit($i,1000)->select()) {
            foreach ($hotelEntityInfos as $hotelEntityInfo) {
                $enddate = strtotime($hotelEntityInfo['enddate']);
                $endLastMonth = strtotime('-1 months', $enddate);
                if ($nowdate === $endLastMonth) {
                    $adminHotelInfo = $adminHotelModel
                        ->where([
                            'hotelentity_id' => $hotelEntityInfo['id'], 
                            'role' => 1,
                        ])
                        ->field('adminlogin_id')
                        ->find();
                    $adminInfo = $adminLoginModel
                        ->where([
                            'id' => $adminHotelInfo['adminlogin_id']
                        ])
                        ->field('areacode,mobile')
                        ->find();
                    if ($adminInfo['areacode'] == '86' && !empty($adminInfo['mobile'])) {
                        $smsSender = new \Org\Util\Sms();
                        $smsSender->apiExpire($adminInfo['mobile'], empty($adminInfo['name']) ? '客户' : $adminInfo['name'], $hotelEntityInfo['name'], date('Y-m-d', $enddate));
                        continue;
                    } elseif ($adminInfo['areacode'] != '86' && !empty($adminInfo['mobile'])) {
                        $yunPian = new \Org\Util\yunPian();
                        $yunPian->apiExpire($adminInfo['areacode'], $adminInfo['mobile'], empty($adminInfo['name']) ? 'client' : $adminInfo['name'], $hotelEntityInfo['name'], date('Y-m-d', $enddate));
                        continue;
                    }
                }
            }
            unset($hotelEntityInfo);
            $i += 1000;
        }
    }

}
